<template>
  <div class="onAccountStatistics">
    <x-header class="header" :left-options="{showBack: showHeaderBack,preventGoBack:true}" @on-click-back="goToPage" ref="header">挂账统计</x-header>
    <div class="topHolder"></div>
    <tab v-model="type">
      <tab-item :disabled="loadingStatus" @on-item-click="changeType">按挂账类型统计</tab-item>
      <tab-item :disabled="loadingStatus" @on-item-click="changeType">按挂账人统计</tab-item>
    </tab>
    <card class="gridDisplay">
      <div class="card-padding" slot="content" style="padding-top: 0;">
        <div class="filterArea dateFilter" ref="filterArea">
          <group-title style="text-align: center;">当前显示日期：{{choosenDate}}<br/></group-title>
          <x-button :show-loading="loadingStatus" :disabled="loadingStatus" @click.native="changeDate(1)" class="inlineBtn">今日</x-button>
          <x-button :show-loading="loadingStatus" :disabled="loadingStatus" @click.native="changeDate(2)" class="inlineBtn">昨日</x-button>
          <a v-if="loadingStatus" class="datePicker"><i class="iconfont icon-rilicalendar107"></i></a>
          <datetime v-else v-model="choosenDate" :start-date="startDate" :end-date="endDate" @on-confirm="changeDate(0)" class="datePicker">
            <i class="iconfont icon-rilicalendar107"></i>
          </datetime>
        </div>
        <h2 class="title">
          ¥ <span v-text="formatCurrency(onAccountInfo.total)">0</span>元<br/>
          <small>总挂账金额</small>
        </h2>
        <grid :cols="2" style="text-align: center;">
          <grid-item label="商品挂账金额">
            <span class="data">¥ <span v-text="formatCurrency(onAccountInfo.goodTotal)">0</span>元</span>
          </grid-item>
          <grid-item label="商品挂账次数">
            <span class="data"><span v-text="onAccountInfo.goodNumber">0</span>次</span>
          </grid-item>
          <grid-item label="破损挂账金额">
            <span class="data">¥ <span v-text="formatCurrency(onAccountInfo.damagedTotal)">0</span>元</span>
          </grid-item>
          <grid-item label="破损挂账次数">
            <span class="data"><span v-text="onAccountInfo.damagedNumber">0</span>次</span>
          </grid-item>
          <grid-item label="会员卡挂账金额">
            <span class="data">¥ <span v-text="formatCurrency(onAccountInfo.memberCardTotal)">0</span>元</span>
          </grid-item>
          <grid-item label="会员卡挂账次数">
            <span class="data"><span v-text="onAccountInfo.memberCardNumber">0</span>次</span>
          </grid-item>
        </grid>
      </div>
    </card>
    <br/>
    <br/>
  </div>
</template>

<script>
  import { XButton, Card, Tab, TabItem, Grid, GridItem, Datetime, GroupTitle, numberComma, dateFormat } from 'vux'
  import {getOnAccountStatistics} from "../../api";
  import {message,toDecimal2} from "../../common/util";

  export default {
    name:'onAccountStatistics',
    data(){
      return{
        showHeaderBack:this.GLOBAL.showHeaderBack,
        loadingStatus:false,
        choosenDate:dateFormat(new Date(), 'YYYY-MM-DD'),
        startDate:'2019-01-01',
        endDate:dateFormat(new Date(), 'YYYY-MM-DD'),
        type:0,
        onAccountInfo:{}
      }
    },
    components:{
      XButton,
      Card,
      Tab,
      TabItem,
      Grid,
      GridItem,
      Datetime,
      GroupTitle
    },
    computed:{
    },
    methods:{
      goToPage(){
        this.$router.push({path:'/'})
      },
      formatCurrency(number){
        return numberComma(toDecimal2(number))
      },
      changeDate(value){
        console.log(value)
        switch (value) {
          case 1:this.choosenDate = dateFormat(new Date(), 'YYYY-MM-DD');break;
          case 2:this.choosenDate = dateFormat(new Date().setDate(new Date().getDate()-1), 'YYYY-MM-DD');break;
          default: break;
        }
        console.log(this.choosenDate)
        this.refreshPage()
      },
      changeType(){
        this.choosenDate = dateFormat(new Date(), 'YYYY-MM-DD')
        this.refreshPage()
      },
      refreshPage(){
        this.loadingStatus = true
        let reqParams = {
          choosenDate:this.choosenDate,
          type:this.type == 0?'onAccountType':'onAccountPerson'
        }
        getOnAccountStatistics(reqParams).then((res)=>{
          if(res.status == 'success'){
            this.onAccountInfo = res.data
          }else if(res.status == 'error') {
            message({message:res.msg,type:'error'})
            console.log('getOnAccountStatistics error info:', res.msg)
          }
          this.loadingStatus = false
        }).catch((err)=>{console.log('getOnAccountStatistics catch info:',err)})
      }
    },
    mounted(){
      this.refreshPage()
      this.$store.commit('updateLoadingStatus',{isLoading:false})
    }
  }
</script>

<style lang="scss">
  .onAccountStatistics{
    .filterArea{
      &.dateFilter{
        margin-bottom: 0.5rem;
        .datePicker{
          float: right;padding: 0;
          &.weui-cell:before{border-top: none;}
          i{font-size: 2rem;color:#535353;}
        }
      }
      .filterItem{margin-top: 0.5rem;}
    }
    .gridDisplay{
      h2.title{
        line-height: 1.8rem;color: #636363;font-size:2rem;text-align: center;margin: 1rem 0;
        small{font-size:1rem;color: #999999;}
      }
      .data{display: inline-block;height:1.5rem;}
    }
  }
</style>
